import { createRouter, createWebHashHistory } from 'vue-router';
import type { RouteLocationNormalized } from 'vue-router';
import { whiteList } from '@/config/routes.ts';
import Nprogress from 'nprogress';
import { getToken } from '@/utils/index.ts';

const router = createRouter({
  history: createWebHashHistory(),
  routes: [
    {
      path: '/login',
      name: 'Login',
      component: () => import('@/pages/login/index.vue'),
      meta: { title: '登录' },
    },
    {
      path: '/',
      name: 'Home',
      component: () => import('@/pages/home/index.vue'),
      meta: { title: '首页' },
    },
    {
      path: '/goodsCategory',
      name: 'GoodsCategory',
      component: () => import('@/pages/goodsCategory/index.vue'),
      meta: { title: '商品分类' },
    },
    {
      path: '/tagsCategory',
      name: 'TagsCategory',
      component: () => import('@/pages/tagsCategory/index.vue'),
      meta: { title: '标签分类' },
    },
    {
      path: '/goodsDetails/:id',
      name: 'Goods',
      component: () => import('@/pages/goodsDetails/index.vue'),
      meta: { title: '商品详情' },
    },
    {
      path: '/store',
      name: 'Store',
      component: () => import('@/pages/store/index.vue'),
      meta: { title: '店铺主页' },
    },
    {
      path: '/car',
      name: 'Car',
      component: () => import('@/pages/car/index.vue'),
      meta: { title: '购物车' },
    },
    {
      path: '/settlement',
      name: 'Settlement',
      component: () => import('@/pages/settlement/index.vue'),
      meta: { title: '确认订单' },
    },
    {
      path: '/allOrder',
      name: 'AllOrder',
      component: () => import('@/pages/allOrder/index.vue'),
      meta: { title: '全部订单' },
    },
    {
      path: '/orderDetail',
      name: 'OrderDetail',
      component: () => import('@/pages/orderDetail/index.vue'),
      meta: { title: '订单详情' },
    },
    {
      path: '/afterSales',
      name: 'AfterSales',
      component: () => import('@/pages/afterSales/index.vue'),
      meta: { title: '售后详情' },
    },
    {
      path: '/address',
      name: 'Address',
      component: () => import('@/pages/address/index.vue'),
      meta: { title: '选择地址' },
    },
    {
      path: '/accountSet',
      name: 'AccountSet',
      component: () => import('@/pages/accountSet/index.vue'),
      meta: { title: '账号设置' },
    },
    {
      path: '/editAddress',
      name: 'EditAddress',
      component: () => import('@/pages/editAddress/index.vue'),
      meta: { title: '新增地址' },
    },
    {
      path: '/register',
      name: 'Register',
      component: () => import('@/pages/register/index.vue'),
      meta: { title: '注册' },
    },
    {
      path: '/userInfo',
      name: 'UserInfo',
      component: () => import('@/pages/userInfo/index.vue'),
      meta: { title: '基本信息' },
    },
    {
      path: '/certificate',
      name: 'Certificate',
      component: () => import('@/pages/certificate/index.vue'),
      meta: { title: '证件资料' },
    },
    {
      path: '/findPassword',
      name: 'FindPassword',
      component: () => import('@/pages/password/index.vue'),
      meta: { title: '找回密码' },
    },
    {
      path: '/changePassword',
      name: 'ChangePassword',
      component: () => import('@/pages/password/changePassword.vue'),
      meta: { title: '找回密码' },
    },
    {
      path: '/policy',
      name: 'Policy',
      component: () => import('@/pages/policy/index.vue'),
      meta: { title: '隐私政策' },
    },
    {
      path: '/clause',
      name: 'Clause',
      component: () => import('@/pages/clause/index.vue'),
      meta: { title: '服务协议' },
    },
    {
      path: '/my',
      name: 'My',
      component: () => import('@/pages/my/index.vue'),
      meta: { title: '我的' },
    },
    {
      path: '/search',
      name: 'Search',
      component: () => import('@/pages/search/index.vue'),
    },
    {
      path: '/searchDetail',
      name: 'SearchDetail',
      component: () => import('@/pages/search/searchDetail.vue'),
    },
    {
      path: '/:pathMatch(.*)*',
      name: 'NotFound',
      component: () => import('@/pages/notFound/index.vue'),
      meta: {
        title: 'NotFound',
      },
    },
  ],
  scrollBehavior() {
    return {
      top: 0, // 相当于之前的y
    };
  },
});
export default router;

// 全局前置守卫
router.beforeEach(async (to, _, next) => {
  Nprogress.start();
  const token = getToken(); // 使用可选链操作符
  if (token) {
    if (to.path === '/login') {
      next({ path: '/', replace: true });
      return;
    }
    next();
  } else {
    if (whiteList.includes(to.path)) {
      next();
    } else {
      next('/login');
    }
  }
});

// 路由后置守卫
router.afterEach((to: RouteLocationNormalized) => {
  if (to.meta.title) {
    document.title = to.meta.title as string;
  }
  Nprogress.done();
});
